package com.hctg.forced2sleep.utils;

import android.app.Activity;
import android.content.Context;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

/**
 * Description: 忽略电池优化工具类 <br />
 * <a href="https://github.com/Blankj/AndroidUtilCode/blob/master/lib/subutil/src/main/java/com/blankj/subutil/util/BatteryUtils.java" target="_blank">BatteryUtils.java</a> <br />
 * @author     : ldf
 * @date       : 2019/12/19 on 18:25
 *
 * @version 1.0
 */
public class BatteryUtils {

    /**
     * 是否忽略电池优化
     * <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
     */
    public static boolean isIgnoringBatteryOptimizations(Context context) {
        return XXPermissions.isGranted(context, Permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
//        boolean isIgnoring = false;
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//Android 6.0
//            PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
//            if (powerManager != null) {
//                isIgnoring = powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
//                return isIgnoring;
//            }
//        }
//        return isIgnoring;
    }

    /**
     * 忽略电池优化
     */
    public static void requestIgnoreBatteryOptimizations(Activity activity, OnPermissionCallback callback) {
        XXPermissions.with(activity)
                .permission(Permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
                .request(callback);
//        try {
//            Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
//            intent.setData(Uri.parse("package:" + activity.getPackageName()));
//            activity.startActivityForResult(intent, requestCode);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
